package jspSamples.unit7.websiteSample;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConnection {
    private String FileName;
    private int DBType;
    private Connection conn;
    private String MySqlDriver;
    private String MySqlURL;

    public DBConnection(){
        conn = null;
        DBType = 1;
    }

    //根据数据库类型创建数据库连接，此处默认类型为1:mysq1
    public Connection getConn(){
        DBType=1;
        switch(DBType){
            case 1:
                return(getConnToMySql());
            default:return null;
        }
    }
    public Connection getConnToMySql() {
        try {
            MySqlDriver = "com.mysql.cj.jdbc.Driver";
            MySqlURL = "jdbc:mysql://localhost:3306/unit7website?user=root&password=operzADF2020qwer&useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai";
            Class.forName(MySqlDriver);
            conn = DriverManager.getConnection(MySqlURL);
            return conn;
        } catch (Exception e) {
            e.printStackTrace();  // 打印异常
        }
        return null;
    }
    public void closeConnection() {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();  // 打印关闭连接时的异常
            }
        }
    }




}
